<template>
  <!-- 弹框 -->
  <el-dialog
    :title="isRouteName ? '修改目录' : '修改标签'"
    :visible="dialogVisibleUpdate"
    width="30%"
    @close="resetForm"
  >
    <el-form
      :model="directorysInfoForm"
      :rules="directorysInfoRules"
      ref="directorysInfoRef"
      label-width="100px"
    >
      <el-form-item label="所属学科" prop="subjectID" minlength="225px">
        <el-select
          v-model="directorysInfoForm.subjectID"
          placeholder="请选择"
          style="width: 100%"
        >
          <el-option
            :label="item.label"
            :value="item.value"
            v-for="item in dataList"
            :key="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        :label="isRouteName ? '目录名称' : '标签名称'"
        :prop="isRouteName ? 'directoryName' : 'tagName'"
        :placeholder="isRouteName ? '请输入目录名称' : '请输入标签名称'"
      >
        <el-input
          v-model="directorysInfoForm.directoryName"
          v-if="isRouteName"
        ></el-input>
        <el-input v-model="directorysInfoForm.tagName" v-else></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="resetForm">取 消</el-button>
      <el-button type="primary" @click="submitInfo">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { update } from '../../api/hmmm/directorys'
import { update as tagUpdate } from '../../api/hmmm/tags'
export default {
  name: 'DirectoryAdd',
  props: {
    isRouteName: {
      type: Boolean,
      require
    },
    dialogVisibleUpdate: {
      type: Boolean,
      require
    },
    dataList: {
      type: Array,
      require
    },
    directoryInfo: {
      type: Object,
      require
    }
  },
  data () {
    return {
      directorysInfoForm: {
        directoryName: '',
        subjectID: '',
        tagName: ''
      },
      directorysInfoRules: {
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ],
        tagName: [
          { required: true, message: '请输入标签名称', trigger: 'blur' }
        ],
        subjectID: [
          { required: true, message: '请输入所属学科', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    directoryInfo (val) {
      deep: true
      this.directorysInfoForm = val
    }
  },
  methods: {
    // 关闭弹框
    resetForm () {
      this.$emit('update:dialogVisibleUpdate', false)
      this.$refs.directorysInfoRef.resetFields()
    },
    // 修改
    submitInfo () {
      this.$refs.directorysInfoRef.validate(async valid => {
        if (valid) {
          try {
            if (this.isRouteName) {
              await update(this.directoryInfo)
            } else {
              await tagUpdate(this.directoryInfo)
            }
            this.$message.success('修改成功')
            this.$parent.getList()
          } catch (error) {
            this.$message.error('修改失败')
          }
        } else {
          console.log('error submit!!')
          return false
        }
        this.resetForm()
      })
    }
  }
}
</script>

<style scoped lang='less'></style>
